package com.turing.post.organization.impl;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.organization.util.OrganApplicationException;
import com.turing.post.organization.util.OrganRespCode;
import com.turing.post.organization.util.VoucherBusinessBind;
import com.turing.post.organization.util.VoucherBusinessBindBusiness;
import com.turing.post.organization.util.VoucherBusinessBindDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author wzd
 * @Dec 18, 2012
 */
@Stateless(mappedName = "organization.impl.VoucherBusinessBindBusinessImpl")
@Remote
@Local
public class VoucherBusinessBindBusinessImpl implements
		VoucherBusinessBindBusiness {

	@EJB(mappedName = "organization.impl.VoucherBusinessBindDaoImpl")
	private VoucherBusinessBindDao voucherBusinessBindImpl;

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;

	public void insertVoucherBusinessBind(PCPSWebInput webInput,
			VoucherBusinessBind voucherbusinessbind)
			throws PCPSApplicationException {
		// 登录用户机构号为空
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "登录用户机构号为NULL");
		}
		// 业务类型
		if (PCPSUtil.isEmpty(voucherbusinessbind.getBusinessType())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"业务类型为空!");
		}
		// 发卡商机构NO
		if (voucherbusinessbind.getVoucherId() == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"凭证为空!");
		}
		// 得到主键id
		VoucherBusinessBind lst = voucherBusinessBindImpl
				.getVoucherBusinessBindByvoucherId(voucherbusinessbind);
		if (lst != null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"业务凭证种类对照信息已经存在!");
		}

		// 插入数据
		voucherBusinessBindImpl.insertVoucherBusinessBind(voucherbusinessbind);
		// 插入数据库操作日志
		insertRecordingOperationLog(webInput, "业务凭证种类对照", voucherbusinessbind,
				SysOperLog.operType_Add, "VoucherId:"
						+ voucherbusinessbind.getVoucherId() + "BusinessType:"
						+ voucherbusinessbind.getBusinessType());
	}

	private void insertRecordingOperationLog(PCPSWebInput webInput,
			String logName, VoucherBusinessBind obj, String operType,
			String lastFieldName) throws PCPSApplicationException {
		String logRemark = PCPSUtil.RecordingOperationLog(obj, lastFieldName);
		if (!PCPSUtil.isEmpty(logRemark)) {
			// 插入数据库日志
			SysOperLog sysOperLog = new SysOperLog();
			if (webInput != null && webInput.getOperId() != null) {
				sysOperLog.setOperId(webInput.getOperId());
				sysOperLog.setOperType(operType);
				sysOperLog.setLogName(logName);
				if (webInput != null) {
					sysOperLog.setIpAddress(webInput.getIpAddress());
				}
				if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
					sysOperLog.setIssueCompanyNo(webInput.getOperBranchNo()
							.substring(0, 6));
					sysOperLog.setBranchNo(webInput.getOperBranchNo());
				}
				sysOperLog.setRemark(logRemark);
				sysOperLogDaoImpl.insertSysOperLog(sysOperLog);
			}
		}
	}
}
